package com.zzzzzz.topic.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zzzzzz.account.signin.NoSuchOnlineUserException;
import com.zzzzzz.account.signin.OnlineService;
import com.zzzzzz.account.user.UserBase;
import com.zzzzzz.topic.NoSuchCommentException;
import com.zzzzzz.topic.comment.Comment;
import com.zzzzzz.topic.comment.CommentDao;

@Service("commentService")
public class CommentServiceImpl implements CommentService {

	@Resource 
	private CommentDao commentDao;
	@Resource
	private OnlineService onlineService;

	public void add(Comment comment) throws NoSuchOnlineUserException {
		UserBase userBase = onlineService.findUserBase();
		comment.setUserBase(userBase);
		commentDao.add(comment);
	}
	
	public Comment add(String userId, String topicId, String parentId, String content) throws NoSuchOnlineUserException {
		Date now = new Date();
		Comment comment = new Comment();
		comment.setTopicId(topicId);
		comment.setContent(content);
		comment.setUserId(userId);
		comment.setCreateDate(now);
		comment.setParentId(parentId);
		
		UserBase userBase = onlineService.findUserBase();
		comment.setUserBase(userBase);
		
		commentDao.add(comment);
		
		return comment;
	}

	public void delete(String commentId) {
		commentDao.delete(commentId);
		
	}

	public Comment findById(String commentId) throws NoSuchCommentException {
		return commentDao.findById(commentId);
	}

	public long findCountByParentId(String parentId) {
		return commentDao.findCountByParentId(parentId);
	}

	public List<Comment> findByParentId(String parentId) {
		return commentDao.findByParentId(parentId);
	}
	
	public List<Comment> findByParentId(String parentId, int start, int offset, boolean ascending) {
		return commentDao.findByParentId(parentId, start, offset, ascending);
	}

	public long findCountByTopicId(String topicId) {
		return commentDao.findCountByTopicId(topicId);
	}
	
	public List<Comment> findByTopicId(String topicId) {
		return commentDao.findByTopicId(topicId);
	}

	public List<Comment> findByTopicId(String topicId, int start, int offset, boolean ascending) {
		return commentDao.findByTopicId(topicId, start, offset, ascending);
	}

	public void editStatus(String commentId, Integer status) {
		commentDao.editStatus(commentId, status);
	}
	
}